package com.tiantianyuegou.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.R;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.util.Tools;

/**
 * 一元购确认收获地址界面
 */
public class OneGouConfirmOrderActivity extends BaseActivity implements View.OnClickListener {
//    色调#b1191a
    private ImageView item_confirm_product_img,confirm_coupon_arraw_img;
    private TextView item_confirm_product_name,item_confirm_product_color,
            item_confirm_product_price,confirm_operation,item_confirm_tip3,
            confirm_consigneeName_txt,confirm_consigneePhone_txt,confirm_receiver_address;
    private EditText item_confirm_usecount_et;
    private RelativeLayout address,confirm_relate_inputCount;
    private boolean hasSelectAddress = false;//是否已经选择收货地址
    private LinearLayout confirm_location_linear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_gou_confirm_order);
        Tools.changeSystemBar(this);
        setTitle("确认订单");
        initViews();
    }

    @Override
    public void findViewByIds() {
        confirm_coupon_arraw_img = (ImageView) findViewById(R.id.confirm_coupon_arraw_img);
        item_confirm_product_img = (ImageView) findViewById(R.id.item_confirm_product_img);
        item_confirm_product_name = (TextView) findViewById(R.id.item_confirm_product_name);
        item_confirm_product_color = (TextView) findViewById(R.id.item_confirm_product_color);
        item_confirm_product_price = (TextView) findViewById(R.id.item_confirm_product_price);
        item_confirm_usecount_et = (EditText) findViewById(R.id.item_confirm_usecount_et);
        item_confirm_tip3 = (TextView) findViewById(R.id.item_confirm_tip3);
        confirm_operation = (TextView) findViewById(R.id.confirm_operation);
        address = (RelativeLayout) findViewById(R.id.address);
        confirm_relate_inputCount = (RelativeLayout) findViewById(R.id.confirm_relate_inputCount);
        confirm_consigneeName_txt = (TextView) findViewById(R.id.confirm_consigneeName_txt);
        confirm_consigneePhone_txt = (TextView) findViewById(R.id.confirm_consigneePhone_txt);
        confirm_receiver_address = (TextView) findViewById(R.id.confirm_receiver_address);
        confirm_location_linear = (LinearLayout) findViewById(R.id.confirm_location_linear);


    }

    @Override
    public void initListener() {
        confirm_coupon_arraw_img.setOnClickListener(this);
        address.setOnClickListener(this);
        confirm_operation.setOnClickListener(this);

    }

    @Override
    public void initData() {
//        获取传入的数据
        Intent intent = getIntent();
        String goodsName = intent.getStringExtra("goodsName");//商品名称
        String count = intent.getStringExtra("count");//剩余量
        String goodsMoney = intent.getStringExtra("goodsMoney");//商品价钱
        String goodsPhoto = intent.getStringExtra("goodsPhoto");//商品图片
        String jifen = intent.getStringExtra("jifen");//积分抵换
        ImageLoader.getInstance().displayImage(goodsPhoto,item_confirm_product_img);
        item_confirm_product_name.setText(goodsName);
        item_confirm_product_color.setText(count);
        item_confirm_product_price.setText("￥"+goodsMoney);
        String trim = item_confirm_usecount_et.getText().toString().trim();
        if (TextUtils.isEmpty(trim)){
            showMsg("请输入积分 ");
        }else{
            item_confirm_tip3.setText(Integer.parseInt(trim)/Integer.parseInt(jifen));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.confirm_coupon_arraw_img:
                confirm_relate_inputCount.setVisibility(View.VISIBLE);
                break;
            case R.id.address:
//                当点击选择地址时，跳转到选择地址接界面
                Intent intent = new Intent(this, SelectAddressActivity.class);
                startActivityForResult(intent,Constant.SELECT_ADDRESS_RESULT);
                break;
            case R.id.confirm_operation:
//                当点击提交订单时跳转到余额支付界面
                Intent intent1 = new Intent(this,PayActivity.class );
                startActivity(intent1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        将选择的地址显示出来

        if (requestCode == Constant.SELECT_ADDRESS_REQUEST) {
            if (resultCode == Constant.SELECT_ADDRESS_RESULT) {
                hasSelectAddress = true;
//                mLinear_noAddress.setVisibility(View.GONE);
                confirm_location_linear.setVisibility(View.VISIBLE);
//                mAddressId = data.getIntExtra("addressId", 0);
                confirm_consigneeName_txt.setText(data.getStringExtra("name"));
                confirm_consigneePhone_txt.setText(data.getStringExtra("phone"));
                confirm_receiver_address.setText(data.getStringExtra("address"));
//                getFreight(data.getStringExtra("cityId"));
            }

        }
    }
}
